import { RecycleApi } from '@/api/infra/file'
const useRecycle = (initInfo, treeRef, resetSelectedInfo) => {
  // 切换到回收站
  const toRecycle = () => {
    resetSelectedInfo()
    initInfo.value.isRecycle = true
    initInfo.value.isOldCard = initInfo.value.isCard
    initInfo.value.isCard = false
    initInfo.value.breadcrumbList = [{ id: 0, title: '回收站' }]
    treeRef.value.setCheckedKeys([], false)
    initInfo.value.recycleInfo.form.page = 1
    initInfo.value.recycleInfo.form.limit = 20
    initInfo.value.recycleInfo.total = 0
    getRecycleSource()
  }

  // 获取回收站数据
  const getRecycleSource = () => {
    RecycleApi.list(initInfo.value.recycleInfo.form).then((res: any) => {
      if (res.code === 200) {
        if (res.data.length > 0) {
          res.data.forEach((item: any) => {
            item.active = false
            item.filesize = parseInt(item.filesize)
            item.fileSize = ''
            if (item.filesize > 0) {
              if (item.filesize / 1024 / 1024 > 1) {
                item.fileSize = Math.floor((item.filesize / 1024 / 1024) * 100) / 100 + 'MB'
              } else {
                item.fileSize = Math.floor((item.filesize / 1024) * 100) / 100 + 'KB'
              }
            } else {
              item.fileSize = '0KB'
            }
            item.fileType = item.mimetype !== '' ? item.mimetype.split('/')[0] : ''
          })
        }
        initInfo.value.sourceList = res.data
        initInfo.value.recycleInfo.total = res.count
        console.log(initInfo.value.sourceList)
      }
    })
  }

  // 页面数据条数改变重新获取数据
  const recycleSizeChange = (val: number) => {
    initInfo.value.recycleInfo.form.limit = val
    initInfo.value.recycleInfo.form.page = 1
    getRecycleSource()
  }

  // 当前页改变重新获取数据
  const recycleCurrentChange = (val: number) => {
    initInfo.value.recycleInfo.form.page = val
    getRecycleSource()
  }

  return {
    toRecycle,
    getRecycleSource,
    recycleSizeChange,
    recycleCurrentChange
  }
}
export default useRecycle
